//
//  RemarkViewController.m
//  Guazi
//
//  Created by koolearn on 11-7-30.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "RemarkInputViewController.h"
#import "Remark.h"
#import "RemarkInputCell.h"
#import "RemarkViewCell.h"
#import "StringUtil.h"
#import "GuaziAppDelegate.h"

@interface RemarkInputViewController (private) 
- (NSMutableArray*) restoreData;
- (void) loadNewRemarks:(time_t)sinceTime;
- (void) processAfterLoadNewRemarks:(NSObject*)obj;
@end


@implementation RemarkInputViewController

@synthesize status;
@synthesize inputRemark;


#pragma mark - loadData methods

#pragma mark - operation methods

- (void) publish
{
    inputRemark = [inputRemark trim];
    if ( [inputRemark length] == 0 ) {
        [GuaziAppDelegate showAlert:@"请输入评论内容"];
        return;
    }
    
    Remark* remark = [[Remark alloc] init];
    remark.text = inputRemark;
    remark.userId = [GuaziAppDelegate getAppDelegate].user.userId;
    remark.statusId = status.statusId;
    remark.type = @"MB";
    remark.source = @"iphone";
    [TwitterClient publishRemarkSync:remark status:status];
    [remark release];
    
    [self.navigationController dismissModalViewControllerAnimated:YES];
    
}


- (void) cancel
{
    [self.navigationController dismissModalViewControllerAnimated:YES];
}


#pragma mark - init & destroy methods

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (id) initWithStatus:(Status*)s
{
    self = [super initWithStyle:UITableViewStylePlain];
    //self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    //self.tableView.separatorColor = [UIColor blackColor];
    if ( self ) {
        self.status = s;
    }
    return self;
}


- (void)dealloc
{
    [status release];
    [super dealloc];
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


#pragma mark - View lifecycle


- (void) buildBarButtonItems
{
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"发布" 
                                                                              style:UIBarButtonItemStyleDone 
                                                                             target:self action:@selector(publish)] autorelease];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)] autorelease];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self buildBarButtonItems];
    self.tableView.backgroundColor = [UIColor colorWithRed:0.95f green:0.95f blue:0.95f alpha:1.0f];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.status = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    RemarkInputCell* cell = (RemarkInputCell*) [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    if ( cell ) {
        [cell.textView becomeFirstResponder];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell0";
    RemarkInputCell* cell = (RemarkInputCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[RemarkInputCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.controller = self;
    return cell;
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 195; 
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (NSIndexPath*) tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

@end
